<template>
  <li @mouseenter.stop="activeId=task.id" @mouseleave.stop="activeId=-1">
    <label>
      <input @change="upTaskListById(task.id)" :checked="task.isChecked" type="checkbox"/>
      <span>{{ task.title }}</span>
    </label>
    <button @click="delTaskListById(task.id)" class="btn btn-danger">删除</button>
  </li>
</template>

<script>
export default {
  name: "TodoItem",
  data() {
    return {
      activeId: -1
    }
  },
  props: {
    task: {
      type: Object,
      required: true
    }
  },
  methods: {
    delTaskListById(id) {
      if (window.confirm("您确定要删除该任务吗？")) {
        // 1
        // const taskList = [...this.taskList];
        // const index = taskList.findIndex(v=>v.id === id);
        // taskList.splice(index,1);
        // this.$emit("update:taskList",taskList);

        // 2
        this.$emit("update:taskList", this.taskList.filter(v => v.id !== id));
      }

    },
    upTaskListById(id) {
      // 1
      this.$emit("update:taskList", this.taskList.map(item => {
        if (item.id === id) item.isChecked = !item.isChecked;
        return item;
      }))

      // 2
      // const taskList = [...this.taskList];
      // const info = taskList.find(v=>v.id=== id);
      // info.isChecked = !info.isChecked;
      // this.$emit("update:taskList",taskList);
    }
  },
}
</script>

<style scoped>
/*item*/
li {
  list-style: none;
  height: 36px;
  line-height: 36px;
  padding: 0 5px;
  border-bottom: 1px solid #ddd;
}

li label {
  float: left;
  cursor: pointer;
}

li label li input {
  vertical-align: middle;
  margin-right: 6px;
  position: relative;
  top: -1px;
}

li button {
  float: right;
  display: block;
  margin-top: 3px;
}

li:before {
  content: initial;
}

li:last-child {
  border-bottom: none;
}

li .done {
  color: #666;
  text-decoration: line-through;
}
</style>